package com.entities.projectitem.list;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.core.managers.LoginManager;
import com.core.protocols.ICustomDatasourceDelegate;
import com.core.protocols.IServiceLayerDelegate;
import com.core.protocols.ITableViewDatasource;
import com.core.protocols.ITableViewDelegate;
import com.core.ui.controller.base.CustomController;
import com.core.ui.customcell.TableViewCell;
import com.core.ui.customcell.TableViewDataCell;
import com.core.ui.customview.TableViewHeaderCell;
import com.core.utils.GlobalConstants;
import com.core.utils.HTMLTagUtils;
import com.core.utils.IndexPath;
import com.core.utils.LocalizableString;
import com.entities.projectitem.servicelayer.ProjectListLayer;
import com.entities.user.servicelayer.UserListLayer;
import com.model.KeyValue;
import com.model.project.ProjectItem;
import com.model.user.User;

public class ProjectListController extends CustomController implements ITableViewDatasource, ITableViewDelegate, IServiceLayerDelegate {

	ArrayList<ProjectItem> projectsList = null;
	private ICustomDatasourceDelegate delegate = null;
	ProjectListLayer projectListLayer = null;
	private String selectedPage = null;
	private boolean editAllowed = false;
	private boolean deleteAllowed = false;

	public String getSelectedPage() {

		return selectedPage;
	}

	public void setSelectedPage(String selectedPage) {

		this.selectedPage = selectedPage;
	}

	/**
	 * ICustomDatasource
	 * 
	 * 
	 */
	@Override
	public void setCustomDatasourceDelegate(ICustomDatasourceDelegate delegate) {

		this.delegate = delegate;

	}

	@Override
	public void reloadData() {
		Set<String> userContextSet = LoginManager.sharedInstance().userContextSet();
		editAllowed = false;
		deleteAllowed = false;
		if (userContextSet != null) {
			editAllowed = userContextSet.contains(GlobalConstants.USERCONTEXT_PROJECTUPDATE);
			deleteAllowed = userContextSet.contains(GlobalConstants.USERCONTEXT_PROJECTDELETE);
		}

		projectListLayer = new ProjectListLayer();
		projectListLayer.setLayerDelegate(this);
		projectListLayer.performRequestWithFilter(null, null);

	}

	/**
     *
     *
     */

	@Override
	public String tableStyleClassName() {

		return null;
	}

	@Override
	public String headerRowStyleClassNameAtIndex(int index) {

		return null;
	}

	@Override
	public String headerCellStyleClassNameAtIndexPath(IndexPath indexPath) {

		return null;
	}

	@Override
	public String cellStyleClassNameAtIndexPath(IndexPath indexPath) {

		return null;
	}

	@Override
	public String rowStyleClassNameAtIndex(int index) {

		return null;
	}

	@Override
	public int numberOfHeadRows() {

		return 1;
	}

	@Override
	public int numberOfHeadCols() {

		return 4;
	}

	@Override
	public TableViewHeaderCell headCellAtIndexPath(IndexPath indexPath) {

		LocalizableString localizableString = LocalizableString.sharedInstance();

		TableViewHeaderCell result = new TableViewHeaderCell();
		result.setStyleName("headerCellSimple");
		switch (indexPath.getCol()) {
		case 0: {
			result.setText("#");
			break;
		}
		case 1: {
			result.setText(localizableString.localizable("entity.ProjectItem.field.name"));
			break;
		}
		case 2: {
			result.setText(localizableString.localizable("entity.ProjectItem.field.reporter"));
			break;
		}
		case 3: {
			result.setText(localizableString.localizable("grid.column.operation"));
			break;
		}

		default:
			break;
		}
		return result;
	}

	@Override
	public int numberOfDataRows() {

		return projectsList.size();
	}

	@Override
	public int numberOfDataCols() {

		return this.numberOfHeadCols();
	}

	@Override
	public TableViewCell dataCellAtIndexPath(IndexPath indexPath) {

		LocalizableString localizableString = LocalizableString.sharedInstance();

		ProjectItem item = projectsList.get(indexPath.getRow());
		TableViewDataCell result = new TableViewDataCell();
		switch (indexPath.getCol()) {
		case 0: {
			result.setStyleName("dataCellSimple");
			result.setText(String.format("%d", 1 + indexPath.getRow()));
			break;
		}
		case 1: {
			result.setStyleName("dataCellSimple maxwidth");
			result.setText(item.getName());
			result.setOnCellClick("%s?%s", GlobalConstants.SERVLET_PROJECTINFO, item.idValue().toString());
			break;
		}
		case 2: {
			String authorText = null;
			result.setStyleName("dataCellSimple");
			String userName = null;
			Integer userId = null;
			User user = item.getAssignee();
			if (user != null) {
				userName = user.getName();
				userId = user.getId();
				String link = String.format("%s?id=%d", GlobalConstants.SERVLET_USERINFO, userId);
				ArrayList<KeyValue> attrs = new ArrayList<KeyValue>();
				attrs.add(new KeyValue("href", link, true));
				attrs.add(new KeyValue("class", "trigger_lnk", true));
				authorText = HTMLTagUtils.customTag("a", attrs, userName);
			} else {
				authorText = "";
			}

			result.setText(authorText);
			break;
		}
		case 3: {

			Set<String> links = new HashSet<String>();
			String idStrValue = item.idValue().getValue();

			if (editAllowed) {
				ArrayList<KeyValue> attrs = new ArrayList<KeyValue>();
				result.setStyleName("dataCellSimple");
				String link = String.format("%s?%s", GlobalConstants.SERVLET_PROJECTUPDATE, item.idValue().toString());
				attrs.add(new KeyValue("href", link, true));
				attrs.add(new KeyValue("class", "trigger_lnk", true));
				links.add(HTMLTagUtils.customTag("a", attrs, localizableString.localizable("object.operation.edit")));
			}

			if (deleteAllowed) {
				String ajax = String.format("javascript:deleteProject(%s);", idStrValue);
				String link = "javascript:void(0);";

				ArrayList<KeyValue> attrs = new ArrayList<KeyValue>();
				attrs = new ArrayList<KeyValue>();
				attrs.add(new KeyValue("href", link, true));
				attrs.add(new KeyValue("class", "trigger_lnk", true));
				attrs.add(new KeyValue("onclick", ajax, true));
				links.add(HTMLTagUtils.customTag("a", attrs, localizableString.localizable("object.operation.delete")));
			}

			StringBuilder stringBuilder = new StringBuilder();
			Iterator<String> iterator = links.iterator();
			int i = 0;
			while (iterator.hasNext()) {
				if (i > 0) {
					stringBuilder.append("|");
				}
				stringBuilder.append(iterator.next());

				i++;
			}
			result.setText(stringBuilder.toString());

			break;

		}
		default: {
			break;
		}
		}
		return result;
	}

	@Override
	public <T> void responseResponseItemsList(final List<T> items, final Object request, final Exception exception) {

		projectsList = new ArrayList<ProjectItem>();

		if (exception != null) {
			return;
		}

		if (items == null) {
			return;
		}

		for (T item : items) {
			projectsList.add((ProjectItem) item);
		}

		if (delegate != null) {
			delegate.refreshContent();
		}
	}

}
